package com.red.star.macalline.data.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.red.star.macalline.data.entity.SysParam;
import org.apache.http.client.fluent.Request;
import org.springframework.util.Assert;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class TicketUtil {

	/**
	 * 优惠券列表
	 *
	 * @param status   -1 全部 0:免费优惠券;1:免费单品券;2:付费优惠券;3:礼品券;4:付费单品券;5:免费券包;6:付费券包
	 * @param page
	 * @param pageSize
	 * @throws IOException
	 */
	public static String ticketList(String mallCode, Integer status, Integer page, Integer pageSize) throws IOException {
		Map<String, String> param = new HashMap<String, String>();
		String sign = SignUtil.fetchSign(param);
		param.put("unit_code", mallCode);
		param.put("status", status.toString());
		param.put("start_row", page.toString());
		param.put("row_count", pageSize.toString());
		Request request = Request.Post(SysParam.MACALLINE_URL + "Foreign/GetTicketList");
		request.setHeader("Content-Type", "application/x-www-form-urlencoded");
		request.setHeader("redstar-call-app-id", SysParam.MACALLINE_APP_ID);
		request.setHeader("redstar-sign", sign);
		String resp = HttpUtil.fetch(request, param);
		return resp;
	}


	/**
	 * 海报列表
	 *
	 * @param omsCode
	 * @return
	 * @throws IOException
	 */
	public static String postList(String omsCode) throws IOException {
		Map<String, String> param = new HashMap<String, String>();
		String sign = SignUtil.fetchSign(param);
		param.put("unit_code", omsCode);
		Request request = Request.Post(SysParam.MACALLINE_URL + "poster/GetPosterList");
		request.setHeader("Content-Type", "application/x-www-form-urlencoded");
		request.setHeader("redstar-call-app-id", SysParam.MACALLINE_APP_ID);
		request.setHeader("redstar-sign", sign);
		String resp = HttpUtil.fetch(request, param);
		return resp;
	}

	/**
	 * 获取海报详情
	 *
	 * @param posterId
	 * @return
	 * @throws IOException
	 */
	public static String posterDetail(String posterId) throws IOException {
		Map<String, String> param = new HashMap<String, String>();
		String sign = SignUtil.fetchSign(param);
		param.put("poster_id", posterId);
		Request request = Request.Post(SysParam.MACALLINE_URL + "poster/GetPosterShareInfo");
		request.setHeader("Content-Type", "application/x-www-form-urlencoded");
		request.setHeader("redstar-call-app-id", SysParam.MACALLINE_APP_ID);
		request.setHeader("redstar-sign", sign);
		String resp = HttpUtil.fetch(request, param);
		return resp;
	}


    /**
     * 获取券详情
     *
     * @param promotionTicketId
     * @return
     * @throws IOException
     */
    public static String getTicketDetail(String promotionTicketId) throws IOException {
        Map<String, String> param = new HashMap<String, String>();
        String sign = SignUtil.fetchSign(param);
        param.put("promotion_ticket_id", promotionTicketId);
        Request request = Request.Post(SysParam.MACALLINE_URL + "Foreign/GetTicketDetail");
        request.setHeader("Content-Type", "application/x-www-form-urlencoded");
        request.setHeader("redstar-call-app-id", SysParam.MACALLINE_APP_ID);
        request.setHeader("redstar-sign", sign);
        String resp = HttpUtil.fetch(request, param);
        return resp;

    }

    /**
     * 领券接口
     *
     * @param promotionTicketId
     * @param vipId
     * @param crmOpenId
     * @return
     * @throws IOException
     */
    public static String coupons(String promotionTicketId, String vipId, String crmOpenId) throws IOException {
        Map<String, String> param = new HashMap<String, String>();
        String sign = SignUtil.fetchSign(param);
        param.put("promotion_ticket_id", promotionTicketId);
        param.put("vip_id", vipId);
        param.put("crm_open_id", crmOpenId);
        Request request = Request.Post(SysParam.MACALLINE_URL + "Foreign/GetTicketDetail");
        request.setHeader("Content-Type", "application/x-www-form-urlencoded");
        request.setHeader("redstar-call-app-id", SysParam.MACALLINE_APP_ID);
        request.setHeader("redstar-sign", sign);
        String resp = HttpUtil.fetch(request, param);
        return resp;

    }

    /**
     * 获取formId
     *
     * @param openId
     * @return
     * @throws IOException
     */
    public static String getFormId(String openId) throws IOException {
        Map<String, String> param = new HashMap<String, String>();
        String sign = SignUtil.fetchSign(param);
        param.put("open_id", openId);
        Request request = Request.Post(SysParam.MACALLINE_URL + "user/getform");
        request.setHeader("Content-Type", "application/x-www-form-urlencoded");
        request.setHeader("redstar-call-app-id", SysParam.MACALLINE_APP_ID);
        request.setHeader("redstar-sign", sign);
        String resp = HttpUtil.fetch(request, param);
        if (!CheckUtil.isEmpty(resp)) {
            JSONObject jsonObject = JSONObject.parseObject(resp);
            if (!CheckUtil.isEmpty(jsonObject)) {
//				return jsonObject.getString("dataMap");
                JSONObject dataMap = jsonObject.getJSONObject("dataMap");
                if (!CheckUtil.isEmpty(dataMap)) {
                    System.err.println(dataMap.getString("vip_cell") + "11111" + dataMap.getString("vip_name"));
                    return dataMap.getString("form_id");
                }
            }
        }
        return null;
    }

    /**
     * 获取formId
     *
     * @param unionId
     * @return
     * @throws IOException
     */
    public static JSONObject getVipInfo(String unionId) throws IOException {
        Map<String, String> param = new HashMap<String, String>();
        String sign = SignUtil.fetchSign(param);
        param.put("union_id", unionId);
        Request request = Request.Post(SysParam.MACALLINE_URL + "user/GetUserByUnionid");
        request.setHeader("Content-Type", "application/x-www-form-urlencoded");
        request.setHeader("redstar-call-app-id", SysParam.MACALLINE_APP_ID);
        request.setHeader("redstar-sign", sign);
        String resp = HttpUtil.fetch(request, param);
        if (!CheckUtil.isEmpty(resp)) {
            JSONObject jsonObject = JSONObject.parseObject(resp);
            if (!CheckUtil.isEmpty(jsonObject)) {
                JSONObject dataMap = jsonObject.getJSONObject("dataMap");
                return dataMap;
            }
        }
        return null;
    }


    /**
     * 获取accessToken
     *
     * @return
     * @throws IOException
     */
    public static String getAccessToken() throws IOException {
        Map<String, String> param = new HashMap<String, String>();
        String sign = SignUtil.fetchSign(param);
        Request request = Request.Post(SysParam.MACALLINE_URL + "Foreign/gettoken");
        request.setHeader("Content-Type", "application/x-www-form-urlencoded");
        request.setHeader("redstar-call-app-id", SysParam.MACALLINE_APP_ID);
        request.setHeader("redstar-sign", sign);
        String back = HttpUtil.fetch(request, param);
        if (!CheckUtil.isEmpty(back)) {
            JSONObject jsonObject = JSONObject.parseObject(back);
            if (!CheckUtil.isEmpty(jsonObject)) {
                return jsonObject.getString("dataMap");
            }
        }
        return null;
    }

    public static String getMallUrl(String omsCode) throws IOException {
        Map<String, String> param = new HashMap<String, String>();
        String sign = SignUtil.fetchSign(param);
        param.put("unit_code", omsCode);
        Request request = Request.Post(SysParam.MACALLINE_URL + "poster/GetPosterList");
        request.setHeader("Content-Type", "application/x-www-form-urlencoded");
        request.setHeader("redstar-call-app-id", SysParam.MACALLINE_APP_ID);
        request.setHeader("redstar-sign", sign);
        String back = HttpUtil.fetch(request, param);
        if (!CheckUtil.isEmpty(back)) {
            JSONObject jsonObject = JSONObject.parseObject(back);
            if (!CheckUtil.isEmpty(jsonObject)) {
                return jsonObject.getString("dataMap");
            }
        }
        return null;
    }

    /**
     * 根据omsCode获取openid,form_id,unit_code
     *
     * @param omsCode
     * @return
     * @throws IOException
     */
    public static JSONArray getOpenInfo(String omsCode) throws IOException {
        Assert.notNull(omsCode, "omsCode for null");
        Map<String, String> param = new HashMap<String, String>();
        String sign = SignUtil.fetchSign(param);
        param.put("unit_code", omsCode);
        Request request = Request.Post(SysParam.MACALLINE_URL + "Foreign/GetPosterFormId");
        request.setHeader("Content-Type", "application/x-www-form-urlencoded");
        request.setHeader("redstar-call-app-id", SysParam.MACALLINE_APP_ID);
        request.setHeader("redstar-sign", sign);
        String back = HttpUtil.fetch(request, param);
        if (!CheckUtil.isEmpty(back)) {
            JSONObject jsonObject = JSONObject.parseObject(back);
            if (!CheckUtil.isEmpty(jsonObject)) {
                return jsonObject.getJSONArray("dataMap");
            }
        }
        return null;
    }

    /**
     * 将已用的formId标记
     *
     * @param openid
     * @param formId
     * @return
     * @throws IOException
     */
    public static String changeStatus(String openid, String formId) throws IOException {
        Assert.notNull(openid, "openid for null");
        Assert.notNull(formId, "formId for null");
        Map<String, String> param = new HashMap<String, String>();
        String sign = SignUtil.fetchSign(param);
        param.put("open_id", openid);
        param.put("form_id", formId);
        Request request = Request.Post(SysParam.MACALLINE_URL + "User/UpdateForm");
        request.setHeader("Content-Type", "application/x-www-form-urlencoded");
        request.setHeader("redstar-call-app-id", SysParam.MACALLINE_APP_ID);
        request.setHeader("redstar-sign", sign);
        String back = HttpUtil.fetch(request, param);
        return back;
    }

    /**
     * 根据券包id获取openid，formid
     *
     * @param cpId
     * @return
     * @throws IOException
     */
    public static JSONArray getInfoByCpId(Integer cpId) throws IOException {
        Assert.notNull(cpId, "null of cpId");
        Map<String, String> param = new HashMap();
        String sign = SignUtil.fetchSign(param);
        param.put("sku_id", String.valueOf(cpId));
        Request request = Request.Post(SysParam.MACALLINE_URL + "Foreign/GetSKuFormId");
        request.setHeader("Content-Type", "application/x-www-form-urlencoded");
        request.setHeader("redstar-call-app-id", SysParam.MACALLINE_APP_ID);
        request.setHeader("redstar-sign", sign);
        String back = HttpUtil.fetch(request, param);
        if (!CheckUtil.isEmpty(back)) {
            JSONObject jsonObject = JSONObject.parseObject(back);
            if (!CheckUtil.isEmpty(jsonObject)) {
                return jsonObject.getJSONArray("dataMap");
            }
        }
        return null;
    }

    /**
     * 海报详情
     *
     * @param posterId
     * @return
     * @throws IOException
     */
    public static JSONObject fecthpPosterDetail(String posterId) throws IOException {
        Map<String, String> param = new HashMap<String, String>();
        String sign = SignUtil.fetchSign(param);
        param.put("poster_id", posterId);
        Request request = Request.Post(SysParam.MACALLINE_URL + "poster/GetPosterDetail");
        request.setHeader("Content-Type", "application/x-www-form-urlencoded");
        request.setHeader("redstar-call-app-id", SysParam.MACALLINE_APP_ID);
        request.setHeader("redstar-sign", sign);
        String resp = HttpUtil.fetch(request, param);
        if (!CheckUtil.isEmpty(resp)) {
            JSONObject jsonObject = JSONObject.parseObject(resp);
            if (!CheckUtil.isEmpty(jsonObject)) {
                return jsonObject.getJSONObject("dataMap");
            }
        }
        return null;
    }

    /**
     * 获取券详情
     *
     * @param ticketId
     * @return
     * @throws IOException
     */
    public static JSONObject fecthpTicketDetail(String ticketId) throws IOException {
        Map<String, String> param = new HashMap<String, String>();
        String sign = SignUtil.fetchSign(param);
        param.put("promotion_ticket_id", ticketId);
        Request request = Request.Post(SysParam.MACALLINE_URL + "ticket/GetTicketInfo");
        request.setHeader("Content-Type", "application/x-www-form-urlencoded");
        request.setHeader("redstar-call-app-id", SysParam.MACALLINE_APP_ID);
        request.setHeader("redstar-sign", sign);
        String resp = HttpUtil.fetch(request, param);
        if (!CheckUtil.isEmpty(resp)) {
            JSONObject jsonObject = JSONObject.parseObject(resp);
            if (!CheckUtil.isEmpty(jsonObject)) {
                return jsonObject.getJSONObject("dataMap");
            }
        }
        return null;
    }

    //TODO 缺少测试app和secret数据
    //TODO 券详情
    //TODO 领券
    //TODO 我的券列表
    //TODO 券包列表-目前缺少此接口


    public static void main(String args[]) throws IOException {
        // System.out.println(fecthpTicketDetail("1490"));
        System.out.println(fecthpPosterDetail("20"));
    }


}
